package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.resutl.RestResponse;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.dto.AddCourseTeacherDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.ICourseTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-06-22
 */
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements ICourseTeacherService {

    @Autowired
    private CourseBaseMapper courseBaseMapper;
    @Autowired
    private CourseTeacherMapper courseTeacherMapper;
    /**
     * 查询课程相关教师接口
     * @param companyId 机构ID
     * @param courseId 课程ID
     * @return
     */
    @Override
    public List<CourseTeacher> getCourseTeacher(Long companyId, Long courseId) {
        //查询课程
        //是否属于自己机构
        checkCourseBaseAndcompany(courseId,companyId);
        //查询该课程的老师
        List<CourseTeacher> courseTeacherList = courseTeacherMapper.selectList(new LambdaQueryWrapper<CourseTeacher>()
                .eq(CourseTeacher::getCourseId, courseId));
        return courseTeacherList;
    }

    /**
     * 添加/修改课程教师接口
     * @param dto 接收添加课程教师信息类
     */
    @Override
    public void saveCourseTeacher(AddCourseTeacherDto dto,Long companyId) {
        //检查课程是否存在，该课程是否属于这个机构
        Long courseId = dto.getCourseId();
        checkCourseBaseAndcompany(courseId, companyId);
        //拷贝
        CourseTeacher courseTeacher = new CourseTeacher();
        BeanUtils.copyProperties(dto,courseTeacher);
        courseTeacher.setCreateDate(LocalDateTime.now());
        //添加课程教师
        boolean saved = saveOrUpdate(courseTeacher);
        if(!saved){
            XueChengPlusException.cast("添加失败！");
        }
    }

    /**
     * 删除教师接口
     * @param courseId 课程id
     * @param teacherId 教师id
     */
    @Override
    public RestResponse deleteCourseTeacher(Long courseId, Long teacherId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(courseBase == null){
            XueChengPlusException.cast("课程不存在");
        }
        CourseTeacher courseTeacher = courseTeacherMapper.selectById(teacherId);
        if(courseTeacher == null || !Objects.equals(courseTeacher.getCourseId(), courseId)){
            XueChengPlusException.cast("教师不存在！");
        }
        int i = courseTeacherMapper.deleteById(teacherId);
        if(i <= 0){
            return RestResponse.validfail("删除失败");
        }
        return RestResponse.success();
    }

    private void checkCourseBaseAndcompany(Long courseId, Long companyId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(courseBase == null){
            XueChengPlusException.cast("课程不存在");
        }
        if(!Objects.equals(courseBase.getCompanyId(), companyId)){
            XueChengPlusException.cast("只能添加自己机构的课程");
        }
    }


}
